package com.huasan.www.demon.rest.resource;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.huasan.www.demon.data.service.AccountinfoService;
import com.huasan.www.demon.data.service.DayDurationService;
import com.huasan.www.demon.data.service.UserdetailService;
import com.huasan.www.demon.pojo.AccountinfoExample;
import com.huasan.www.demon.pojo.DayDuration;
import com.huasan.www.demon.pojo.DayDurationExample;
import com.huasan.www.demon.pojo.SumAccountStatus;
import com.huasan.www.demon.pojo.SumDetailStatus;
import com.huasan.www.demon.pojo.UserdetailExample;

@Path("/dash")
public class DashResource extends Resource{

	@Autowired
	private AccountinfoService accountinfoService;
	
	@Autowired
	private UserdetailService userdetailService;
	
	@Autowired
	private DayDurationService dayDurationService;
	
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
	
	private Logger logger = LoggerFactory.getLogger(DashResource.class);
	
	@GET
	@Path("/userlogincount")
	@Produces({MediaType.APPLICATION_JSON} )
	public String getUserDetailCount(){
		try{
			UserdetailExample example = new UserdetailExample();
			List<SumDetailStatus> list = userdetailService.selectGroupByStatusExample(example);
			return getSucReturn(objectmapper.writeValueAsString(list), ""); 
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return getErrReturn("查询登录用户信息失败");
		}
	}
	
	@GET
	@Path("/userloginmoncount")
	@Produces({MediaType.APPLICATION_JSON} )
	public String getUserDetailLoginMonCount(){
		try{
			UserdetailExample example = new UserdetailExample();
			UserdetailExample.Criteria criteria = example.createCriteria();
			Timestamp current = new Timestamp(System.currentTimeMillis());
			Timestamp firstMon = get1stDayOfMon(0);
			criteria.andLogintimeBetween(firstMon, current);
			Integer count = userdetailService.countByExample(example);
			return getSucReturn(count.toString(), ""); 
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return getErrReturn("查询本月登录用户失败");
		}
	}
	
	@GET
	@Path("/userdurationmoncount")
	@Produces({MediaType.APPLICATION_JSON} )
	public String getUserDetailDuraMonCount(){
		try{
			UserdetailExample example = new UserdetailExample();
			UserdetailExample.Criteria criteria = example.createCriteria();
			Timestamp firstMon = get1stDayOfMon(0);
			criteria.andLogintimeGreaterThanOrEqualTo(firstMon);
			Integer count = userdetailService.sumDuraByExample(example);
			return getSucReturn(count.toString(), ""); 
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return getErrReturn("查询本月登录时间失败");
		}
	}
	
	@GET
	@Path("/useraccountcount")
	@Produces({MediaType.APPLICATION_JSON} )
	public String getAccountCount(){
		try{
			AccountinfoExample example = new AccountinfoExample();
			List<SumAccountStatus> list = accountinfoService.selectGroupByStatusExample(example);
			return getSucReturn(objectmapper.writeValueAsString(list), ""); 
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return getErrReturn("查询账号使用信息失败");
		}
	}
	
	@GET
	@Path("/useraccountoptcount")
	@Produces({MediaType.APPLICATION_JSON} )
	public String getAccountOptCount(){
		try{
			AccountinfoExample example = new AccountinfoExample();
			List<SumAccountStatus> list = accountinfoService.selectGroupByOptExample(example);
			return getSucReturn(objectmapper.writeValueAsString(list), ""); 
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return getErrReturn("查询账号分布信息失败");
		}
	}
	
	@GET
	@Path("/userdaymoncount")
	@Produces({MediaType.APPLICATION_JSON} )
	public String getMonthDayCount(){
		try{
			Map map = new HashMap();
			map.put("current", map2List(getDuraOfDayMon(0)));
			map.put("last", map2List(getDuraOfDayMon(-1)));
			return getSucReturn(objectmapper.writeValueAsString(map), ""); 
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return getErrReturn("查询上月对比失败");
		}
	}
	
	private Timestamp get1stDayOfMon(Integer diff){
		Calendar cal = Calendar.getInstance();
		cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.MONTH, diff);
		Timestamp time = new Timestamp(cal.getTime().getTime());
		time.setHours(0);
		time.setMinutes(0);
		time.setSeconds(0);
		return time;
	}
	
	private Map<String,Integer> GenerateArray(List<DayDuration> duras){
		Map<String, Integer> map = new LinkedHashMap<String, Integer>();
		for(Integer i=1; i<32; i++){
			map.put(i<10?"0"+i.toString():i.toString(), 0);
		}
		for (DayDuration d : duras){
			String key = d.getDate().substring(d.getDate().length()-2, d.getDate().length());
			map.put(key, d.getMin().intValue());
		}
		return map;
	}
	
	private Map<String, Integer> getDuraOfDayMon(Integer diff){
		DayDurationExample example = new DayDurationExample();
		DayDurationExample.Criteria criteria = example.createCriteria();
		Timestamp firstMon = get1stDayOfMon(diff);
		criteria.andDateLike(dateFormat.format(firstMon)+"%");
		List<DayDuration> list = dayDurationService.selectByExample(example);
		return GenerateArray(list);
	}
	
	private List map2List(Map<String, Integer> map){
		List list = new ArrayList();
		for (Entry<String, Integer> e : map.entrySet()){
			String[] s = new String[2];
			s[0] = e.getKey();
			s[1] = e.getValue().toString();
			list.add(s);
		}
		return list;
	}
}
